<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="queryParams.title" placeholder="请输入标题" clearable size="small"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
                     v-hasPermi="['content:movementLink:query']"
          >搜索
          </el-button>
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                     v-hasPermi="['content:movementLink:add']"
          >新增
          </el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="linkList" border>
        <el-table-column label="ID" align="center" prop="id" v-if="false"/>
        <el-table-column label="组织ID" align="center" prop="deptId" v-if="false"/>
        <el-table-column label="标题" align="center" width="200" prop="title" :show-overflow-tooltip="true"/>
        <el-table-column label="内容" align="center" prop="htmlText" :show-overflow-tooltip="true"/>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180px"/>
        <el-table-column label="操作" align="center" width="250">
          <template slot-scope="scope">
            <el-button size="mini" type="text"
                       icon="el-icon-view"
                       @click="previewPage(scope.row)"
                       @mouseenter.native="mouseOverPreview(scope.row)"
                       @mouseleave.native="mouseOutPreview"
                       v-hasPermi="['content:movementLink:preview']"
            >预览
            </el-button>
            <el-button size="mini" type="text"
                       icon="el-icon-view"
                       @click="copyLink(scope.row)"
                       v-hasPermi="['content:movementLink:preview']"
            >复制
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['content:movementLink:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['content:movementLink:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>

    <div
      style="position: absolute;top: 50%;left:50%;transform: translate(-50%, -50%);"
      v-show="showQrCode"
    >
      <div ref="qrCodeDiv"/>
    </div>

    <el-dialog :title="title" :visible.sync="open" append-to-body
               :before-close="closeDialogConfirm" :width="dialogWidth"
    >
      <el-form label-width="68px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题">
              <el-input v-model="form.title" placeholder="请输入标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <myEditor :content="form.htmlText" @syncContent="syncContent"/>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import myEditor from '@/views/components/richTextEditor'
import QRCode from 'qrcodejs2'
import { addMovementLink, delMovementLink, listMovementLink, updateMovementLink } from '@/api/system/movementLink'

export default {
  name: 'index',
  components: { myEditor },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      linkList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        title: undefined
      },
      // 表单参数
      form: {
        id: undefined,
        deptId: undefined,
        title: '',
        htmlText: ''
      },
      // 对话框宽度
      dialogWidth: '450px',
      // 页面的地址前缀
      pagePath: 'http://manage.zyzncloud.com:8989/movementLink/getPage/',
      pageLinkQrCode: undefined,
      showQrCode: false
    }
  },
  created() {
    this.getList()
  },

  mounted() {
    this.setDialogWidth()
    window.onresize = () => {
      return (() => {
        this.setDialogWidth()
      })()
    }

    this.pageLinkQrCode = new QRCode(this.$refs.qrCodeDiv, {
      text: '',
      width: 200,
      height: 200,
      colorDark: '#333333',
      colorLight: '#ffffff'
    })
  },

  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listMovementLink(this.queryParams).then(response => {
        this.linkList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deptId: undefined,
        title: '',
        htmlText: ''
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加动态内容'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true
      this.title = '修改动态内容'
    },
    /** 提交按钮 */
    submitForm: function() {
      if (this.form.id !== undefined) {
        updateMovementLink(this.form).then(response => {
          if (response.code === 200) {
            this.msgSuccess('修改成功')
            this.open = false
            this.getList()
          }
        })
      } else {
        addMovementLink(this.form).then(response => {
          if (response.code === 200) {
            this.msgSuccess('新增成功')
            this.open = false
            this.getList()
          }
        })
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delMovementLink(id)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    },
    /**
     * 对话框确认关闭
     */
    closeDialogConfirm() {
      this.$confirm('确认关闭?').then(() => {
        this.open = false
      }).catch(() => {
      })
    },

    // 获取页面链接
    getPageLink(id) {
      return `${this.pagePath}${id}`
    },

    // 预览页面
    previewPage(row) {
      window.open(this.getPageLink(row.id))
    },

    // 复制链接
    copyLink(row) {
      let input = document.createElement('input')
      input.value = this.getPageLink(row.id)
      document.body.appendChild(input)
      input.select()
      document.execCommand('Copy')
      document.body.removeChild(input)
      this.msgSuccess('已复制链接')
    },

    // 富文本同步数据函数
    syncContent(data) {
      this.form.htmlText = data
    },
    /**
     * 设置宽度为浏览器的60%
     */
    setDialogWidth() {
      let width = document.body.clientWidth
      this.dialogWidth = parseInt(width * 0.6) + 'px'
    },
    mouseOverPreview(row) {
      let link = this.getPageLink(row.id)
      this.pageLinkQrCode.makeCode(link)
      this.showQrCode = true
    },
    mouseOutPreview() {
      this.showQrCode = false
      this.pageLinkQrCode.clear()
    }
  }
}
</script>

<style scoped>
.el-table::before {
  height: 0;
}
</style>
